import time
from pathlib import Path
import os
from datetime import datetime, timedelta

import pandas as pd
import requests
from dotenv import load_dotenv
load_dotenv()

def _build_output_csv(dir_path: str, start_date: str, end_date: str) -> str:
    """根据日期范围生成输出 CSV 路径，位于项目 data 目录。"""
    os.makedirs(dir_path, exist_ok=True)
    filename = f"weather_{start_date}_{end_date}.csv"
    return os.path.join(dir_path, filename)


# 主函数
if __name__ == "__main__":
    # 环境变量和初始化
    dir_path = "../data/raw"
    app_key = os.getenv("SHENZHEN_OPEN_DATA_APP_KEY")
    page_num = 1
    rows = 4000
    # 日期范围 可以选择你要爬取的范围
    startDate = "20210501"
    endDate = "20210514"
    url = "https://opendata.sz.gov.cn/api/1621236807/1/service.xhtml"
    # 请求头 不加请求会被拒绝
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36",
        "Accept": "application/json, text/javascript, */*; q=0.01",
    }

    # 数据请求和处理循环
    start_dt = datetime.strptime(startDate, "%Y%m%d")
    end_dt = datetime.strptime(endDate, "%Y%m%d")
    delta = end_dt - start_dt
    days = delta.days + 1
    print(f"请求天数为: {days} 天")

    output_csv = Path(_build_output_csv(dir_path, startDate, endDate))

    while True:
        params = {
            "appKey": app_key,
            "page": page_num,
            "rows": rows,
            "startDate": startDate,
            "endDate": endDate,
        }
        response = requests.get(url, headers=headers, params=params)
        # print("请求 URL:", response.url)
        # print("响应状态:", response.status_code, " Content-Type:", response.headers.get("Content-Type"))
        # print(f"响应状态:{response.status_code}")

        if response.status_code != 200:
            print(f"请求错误，状态码：{response.status_code}")
            break

        items = response.json().get("data", [])
        if not items:
            print("没有更多数据或数据为空，结束。")
            break

        # 将本页数据追加写入 CSV
        df = pd.DataFrame(items)
        write_header = (page_num == 1) and (not output_csv.exists())
        df.to_csv(
            output_csv, mode="a", index=False, encoding="utf-8", header=write_header
        )
        print(f"已写入第 {page_num} 页，共 {len(df)} 条。")

        # 判断是否继续
        if len(items) < rows:
            print("最后一页已写完。")
            break
        else:
            page_num += 1
            # 轻微休眠，避免请求过快
            time.sleep(0.5)
    start_dt += timedelta(days=1)
    startDate = start_dt.strftime("%Y%m%d")
    page_num = 1